<template>
  <div class="personal">
    <Pane>
      <span slot="title">个人中心</span>
      <section slot="content">
        <div class="personal-info">
          <ul>
            <li class="personal-info-item">
              <label>管理员ID：</label>
              <span>{{user.id}}</span>
            </li>
            <el-divider></el-divider>
            <li class="personal-info-item">
              <label>账号：</label>
              <span>{{user.account}}</span>
            </li>
            <el-divider></el-divider>
            <li class="personal-info-item">
              <label>用户组：</label>
              <span>{{user.userGroup}}</span>
            </li>
            <el-divider></el-divider>
            <li class="personal-info-item">
              <label>创建时间：</label>
              <span>{{user.ctime | filterCtime}}</span>
            </li>
            <el-divider></el-divider>
            <li class="personal-info-item">
              <label class="img-url">管理员头像：</label>
              <el-upload
                class="avatar-uploader"
                :show-file-list="false"
                action="http://127.0.0.1:5000/users/avatar_upload"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="imgUrl" :src="imgUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </li>
            <el-divider></el-divider>
            <li>
              <el-button @click="handerSave" type="success">保存</el-button>
            </li>
          </ul>
        </div>
      </section>
    </Pane>
  </div>
</template>

<script>
// 引入Pane
import Pane from "@/components/Pane/Pane";
// 数据接口
import { getPersonalInfo, getPersonalAvatarEdit } from "@/api/account";
// 引入工具函数
import local from "@/utils/local";

// moment
import moment from "moment";
export default {
  // 注册
  components: {
    Pane,
  },
  data() {
    return {
      imgUrl: "",
      imgUrlNew: "",
      user: {},
      imgBaseUrl: "http://127.0.0.1:5000/upload/imgs/acc_img/",
    };
  },
  created() {
    this.user = local.get("user");
  },
  filters: {
    filterCtime: function (time) {
      return moment(time).format("YYYY-MM-DD HH:MM:SS");
    },
  },
  methods: {
    // 图片上传
    handleAvatarSuccess(res) {
      // [object object] --> SON.stringify()
      // console.log("handleAvatarSuccess:" + JSON.stringify(res));
      let { code, msg, imgUrl } = res;
      console.log(code, msg, imgUrl);
      if (code === 0) {
        this.$message.success(msg);
        this.imgUrl = this.imgBaseUrl + imgUrl;
        this.imgUrlNew = imgUrl;
      }
    },
    // 上传图片
    beforeAvatarUpload(file) {
      console.log(file);
      // 图片类型
      /* 
      name: "e836d817685fb2cb12c4fe77219e2a9f.jpeg"
      size: 225473
      type: "image/jpeg"
      uid: 1596164570252
      webkitRelativePath: ""
      */
      //  ==  和 === 区别？？
      const isJPG =
        file.type === "image/jpeg" ||
        file.type === "image/jpg" ||
        file.type === "image/png" ||
        file.type === "image/gif" ||
        file.type === "image/bmp" ||
        file.type === "image/webp";

      const isLt4M = file.size / 1024 / 1024 < 4; // 限制大小
      if (!isJPG) {
        this.$message.error("上传的图片只能是有jpeg,jpg,png,gif,bmp,webp");
      }
      if (!isLt4M) {
        this.$message.error("上传的图片最大是4M");
      }
      return isJPG && isLt4M;
    },

    async handerSave() {
      let data = await getPersonalAvatarEdit({
        imgUrl: this.imgUrlNew,
      });
      if (data.code === 0) {
        this.$bus.$emit("update_avatar");
        this.$message.success("上传成功");
        location.reload();
      }
    },
    // 获取信息
    // async getPersonalInfo() {
    //   let data = await getPersonalInfo();
    //   console.log(data);
    //   this.user = data;
    // },
  },
};
</script>

<style lang="less" scoped>
.avatar-uploader {
  margin-top: 20px;
}
/deep/ .avatar-uploader .el-upload {
  border: 2px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/ .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
/deep/ .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
/deep/ .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.img-url {
  margin: 20px 0;
}
</style>